package model.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.dto.Tournament;
import utility.Database;
import utility.Log;

public class TournamentModel extends Database {
	public boolean insert(Tournament dto) {
		String sql = "insert into tbl_tournament(tournament_id,tournament_name,tournament_type,league_id) values(null,?,?,?)";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, dto.getName());
			pstmt.setInt(2, dto.getType());
			pstmt.setInt(3, dto.getLeagueId());
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}

		return false;
	}

	public boolean delete(int tournament_id) {
		String sql = "delete from tbl_tournament where tournament_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, tournament_id);
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return false;
	}

	public boolean update(Tournament dto) {
		String sql = "update tbl_tournament set tournament_name=?, tournament_type=?,league_id=? where tournament_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, dto.getName());
			pstmt.setInt(2, dto.getType());
			pstmt.setInt(3, dto.getLeagueId());
			pstmt.setInt(4, dto.getId());
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return false;
	}

	public Tournament get(int tournament_id) {
		String sql = "select * from tbl_tournament where tournament_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, tournament_id);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				Tournament dto = new Tournament();
				dto.setId(tournament_id);
				dto.setName(rs.getString("tournament_name"));
				dto.setType(rs.getInt("tournament_type"));
				dto.setLeagueId(rs.getInt("league_id"));
				return dto;
			}
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return null;
	}

	public ResultSet list(String search, int limit, int page) {
		String sql = "select * from tbl_tournament where tournament_name like ? ORDER BY tournament_id DESC limit ?,?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "%" + search + "%");
			pstmt.setInt(2, (page - 1) * limit);
			pstmt.setInt(3, limit);
			ResultSet rs = pstmt.executeQuery();
			return rs;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return null;
	}

	public ResultSet list(int limit, int page) {
		return list("", limit, page);
	}

	public int count(String search) {
		String sql = "select count(*) from tbl_tournament where tournament_name like ?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "%" + search + "%");
			ResultSet rs = pstmt.executeQuery();
			return rs.next() ? rs.getInt(1) : 0;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return 0;
	}

	public int count() {
		return count("");
	}
public static void main(String[] args) {
	Tournament dto=new Tournament();
	dto.setName("Spain Cup777");
	dto.setType(1);
	dto.setLeagueId(3);
	dto.setId(2);
	TournamentModel dao=new TournamentModel();
	//System.out.println(dao.insert(dto));
	System.out.println(dao.delete(2));
	//System.out.println(dao.update(dto));
}
}
